//
//  EditSelfManager.m
//  17DianYing
//
//  Created by apple on 15/9/24.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "EditSelfManager.h"
#import "AFNetworking.h"


@interface EditSelfManager()

@property(nonatomic,strong)AFHTTPRequestOperationManager *afManager;


@end



@implementation EditSelfManager

+(instancetype)sharManager
{
    static EditSelfManager *m = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        m = [[EditSelfManager alloc]init];
    });
    return m;
}


-(void)getHeadImagerWithToken:(NSString *)token andData:(NSData *)data andTyp:(NSString *)typ andBlock:(editSelfBlock)hb
{
    
    // 设置请求时间
//    self.afManager.requestSerializer.timeoutInterval = 10;

//传图片
    
    NSData *imageData = data;
NSString *URL =[http stringByAppendingString:@"/mobile/user/center/uploadPic"];
NSMutableURLRequest *request =[[AFHTTPRequestSerializer serializer]
                               multipartFormRequestWithMethod:@"POST"
                               URLString:URL
                               parameters:@{@"file":[NSString stringWithFormat:@"123.png"],@"token":token,@"type":typ}
                               constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
                               {
                                   [formData appendPartWithFileData:imageData
                                                               name:@"file"
                                                           fileName:@"file"
                                                           mimeType:@"image/png"];
                               }];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    }
    else {

        
        NSDictionary *dic = responseObject;
        hb(dic);
        NSLog(@"%@",dic);

    }
}];
[uploadTask resume];

}

-(void)setSelfWithSex:(NSString *)getSex andNickName:(NSString *)nickName andToken:(NSString *)token andBlock:(editSelfBlock)hb
{
    self.afManager = [[AFHTTPRequestOperationManager alloc]init];
    self.afManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString * url =[http stringByAppendingString:@"/mobile/user/center/updateMyUserInfo"];
    NSDictionary * dic =  @{@"token":token,@"sex":getSex,@"nickName":nickName};
    
    AFHTTPRequestOperation *op = [self.afManager POST:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        id obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSDictionary *dic = obj;
        hb(dic);
        
        NSLog(@"=================================%@",dic);
        [op start];

        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
    
    
}










@end
